﻿namespace gui {
    using System.Collections.ObjectModel;
    using snat.model;
    using System.Collections.Generic;
    using System.ComponentModel;

    /// <summary>
    /// The All Messages List UI component of the Node Properties Tab
    /// </summary>
    public partial class MessageTotalListView {

        public static event guiNodeQueryFinishedEvent NodeQueryFinishedEvent;
        private bool receivedFirstList = false;
        private List<MessageItem> messageBuffer;
        private ObservableCollection<MessageItem> messageList;
        public ObservableCollection<MessageItem> MessageList {
            get { return messageList; }
        }

        private BaseNode nodeSelected;
        public BaseNode NodeSelected {
            get { return nodeSelected; }
        }

        public MessageTotalListView() {
            InitializeComponent();

            // Set up messasges list for cross-thread binding
            messageList = new ObservableCollection<MessageItem>();
            messageBuffer = new List<MessageItem>();
            DispatchingList<ObservableCollection<MessageItem>, MessageItem> dispatchingList = new DispatchingList<ObservableCollection<MessageItem>, MessageItem>(messageList, Dispatcher);
            MessageListXAML.ItemsSource = dispatchingList;

            NetworkModel.Instance.MessageProcessed += guiOnMessageSend;
            NodeListView.NodeSelectedEvent += guiOnNodeSelectionChanged;
            MessageSentListView.BuiltMessageListEvent += guiOnMessageListReceived;
            MessageReceivedListView.BuiltMessageListEvent += guiOnMessageListReceived;
        }

        private void addToMessageList(Message msg) {
            if (msg.Sender.ID == nodeSelected.ID) {
                foreach (BaseNode e in msg.Recipients) messageList.Add(new MessageItem(msg, e.Name));
            } else {
                messageList.Add(new MessageItem(msg, nodeSelected));
            }
        }

        public void guiOnMessageSend(Message m) {
            if (nodeSelected != null) {
                foreach (BaseNode e in m.Recipients) {
                    if (e.ID == nodeSelected.ID) {
                        addToMessageList(m);
                    }
                }
                if (m.Sender.ID == nodeSelected.ID) {
                    addToMessageList(m);
                }
            }
        }

        // Builds Total Messages List
        public void guiOnMessageListReceived(BaseNode node, ObservableCollection<MessageItem> list) {

            // We are dealing with the first list
            if (receivedFirstList == false) {
                foreach (MessageItem m in list) messageBuffer.Add(m);
                nodeSelected = node;
                receivedFirstList = true;

            } else {
                // We are dealing with the second list
                messageBuffer.Sort();
                messageList.Clear();
                foreach (MessageItem m in messageBuffer) messageList.Add(m);

                // Reset everything for next nodeSelection-changed event
                receivedFirstList = false;
                messageBuffer.Clear();

                // Fire NodeQueryFinished Event
                if (NodeQueryFinishedEvent != null) NodeQueryFinishedEvent();
            }
        }

        // Clear current list when the user selected a new node
        public void guiOnNodeSelectionChanged() {
            messageList.Clear();
        }

    }
}